package Controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entity.Room;
import Entity.Timetable;
import Service.RoomService;
import Service.TimetableService;

/**
 * Servlet implementation class CheckTimetableServlet
 */
@WebServlet("/CheckTimetableServlet")
public class CheckTimetableServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckTimetableServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String mid=request.getParameter("mid");
		String mname=request.getParameter("mname");
		String op=request.getParameter("op");
		if("del".equals(op)){
			String id=request.getParameter("id");
			int num=0;
			if(id!=null&&id!="") num=Integer.parseInt(id);
			TimetableService tmp=new TimetableService();
			tmp.deleteTimetable(num);
		}else if("add1".equals(op)){
			RoomService ser=new RoomService();
			List<Room>list=ser.findAllRoom();
			request.setAttribute("list", list);
			request.setAttribute("mid", mid);
			request.setAttribute("mname", mname);
			request.getRequestDispatcher("/WEB-INF/page/AddTimetable.jsp").forward(request, response);
		}
		else if("add2".equals(op)){
			String roomid=request.getParameter("roomid");
			String beg=request.getParameter("beg");
			String end=request.getParameter("end");
			String day=request.getParameter("day");
			String section=request.getParameter("section");
			Timetable table=new Timetable();
			if(roomid!=null&&roomid!="") table.setRoomid(roomid);
			if(beg!=null&&beg!="") table.setBegweek(Integer.parseInt(beg));
			if(end!=null&&end!="") table.setEndweek(Integer.parseInt(end));
			if(day!=null&&day!="") table.setDay(Integer.parseInt(day));
			if(section!=null&&section!="") table.setSection(Integer.parseInt(section));
			TimetableService s=new TimetableService();
			List<Timetable> tables=new ArrayList<Timetable>();
			boolean isok=true;
			//逻辑有问题
			for(int i=Integer.parseInt(beg);i<=Integer.parseInt(end)&&isok;i++){
				List<Timetable> tmp=s.findByTime(i, Integer.parseInt(day), Integer.parseInt(section));
				if(tmp==null) isok=true;
				for(int j=0;j<tmp.size()&&isok;j++){
					Timetable t=tmp.get(j);
					if(t.getRoomid()==roomid){
						isok=false;
						break;
					}
				}
			}
			if(isok)
			s.addTimetable(table);
		}
		TimetableService ser=new TimetableService();
		List<Timetable> list=(List<Timetable>)ser.findAllTimetable();
		request.setAttribute("mid", mid);
		request.setAttribute("mname", mname);
		
		request.setAttribute("list", list);
		request.getRequestDispatcher("/WEB-INF/page/CheckTimetable.jsp").forward(request, response);
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
	}

}
